package com.atguigu.gmallpublisher1.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmallpublisher1.service.ProductStatsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/sugar")
public class SugarController {

    @Autowired
    private ProductStatsService productStatsService;

    @RequestMapping("/gmv")
    public String getGmv(@RequestParam(value = "date", defaultValue = "0") int date) {

        System.out.println("Success");

        //如果外部没有传输时间参数,则取当天时间
        if (date == 0) {
            date = getToday();
        }

        //查询ClickHouse获取GMV
        BigDecimal gmv = productStatsService.selectGmv(date);

        //封装JSON字符串
        String result = "{" +
                "\"status\": 0," +
                "\"msg\": \"\"," +
                "\"data\": " + gmv +
                "}";

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("status", 0);
        resultMap.put("msg", "");
        resultMap.put("data", gmv);

        //返回结果
        return JSONObject.toJSONString(resultMap);
    }


    /**
     * {
     * "status": 0,
     * "msg": "",
     * "data": {
     * "categories": [
     * "苹果",
     * "三星",
     * "华为",
     * "oppo",
     * "vivo",
     * "小米83"
     * ],
     * "series": [
     * {
     * "name": "手机品牌",
     * "data": [
     * 7576,
     * 8141,
     * 7091,
     * 5753,
     * 9080,
     * 7455
     * ]
     * }
     * ]
     * }
     * }
     */
    @RequestMapping("trademark")
    public String getGmvByTm(@RequestParam(value = "date", defaultValue = "0") int date,
                             @RequestParam(value = "limit", defaultValue = "5") int limit) {

        //1.查询数据
        if (date == 0) {
            date = getToday();
        }
        Map map = productStatsService.selectGmvByTm(date, limit);

        //2.封装JSON数据
        String result = "{" +
                "\"status\": 0," +
                "\"msg\": \"\"," +
                "\"data\": {" +
                "\"categories\": [\"" +
                StringUtils.join(map.keySet(), "\",\"") +
                "\"]," +
                "\"series\": [" +
                "{" +
                "\"name\": \"商品品牌\"," +
                "\"data\": [" +
                StringUtils.join(map.values(), ",") +
                "]" +
                "}" +
                "]" +
                "}" +
                "}";
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("status", 0);
        resultMap.put("msg", "");

        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("categories", map.keySet());

        ArrayList<Map> maps = new ArrayList<>();
        HashMap<String, Object> seriesMap = new HashMap<>();
        seriesMap.put("name", "商品品牌");
        seriesMap.put("data", map.values());
        maps.add(seriesMap);
        dataMap.put("series", maps);

        resultMap.put("data", dataMap);

        //3.返回结果
        return JSON.toJSONString(resultMap);
    }

    private int getToday() {
        long ts = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return Integer.parseInt(sdf.format(ts));
    }

}
